import { EChartsOption } from "echarts-for-react";
import { PieChartConfig } from "./index";
import { defaultGrid, defaultTooltip, defaultLegend } from "../utils";

function transform(data: any[], config: PieChartConfig): EChartsOption {
  const { labelField, valueField } = config;

  return {
    grid: {
      ...defaultGrid,
    },
    legend: {
      ...defaultLegend,
    },
    tooltip: {
      ...defaultTooltip,
    },
    series: [
      {
        type: "pie",
        data: data.map((t) => ({ name: t[labelField], value: t[valueField] })),
      },
    ],
  };
}

export default transform;
